import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Layout from '../views/Layout.vue'
import Dashboard from '../views/Dashboard.vue'
import UserManagement from '../views/UserManagement.vue'
import RoleManagement from '../views/RoleManagement.vue'
import OrganizationManagement from '../views/OrganizationManagement.vue'
import TeacherManagement from '../views/TeacherManagement.vue'
import StudentManagement from '../views/StudentManagement.vue'
import NoticeManagement from '../views/NoticeManagement.vue'
import NoticeView from '../views/NoticeView.vue'
import CourseManagement from '../views/CourseManagement.vue'
import CourseTemplateManagement from '../views/CourseTemplateManagement.vue'
import CourseApplication from '../views/teacher/CourseApplication.vue'
import MyApplications from '../views/teacher/MyApplications.vue'
import CourseApplicationReview from '../views/admin/CourseApplicationReview.vue'
import ScheduleManagement from '../views/teacher/ScheduleManagement.vue'
import AllSchedules from '../views/admin/AllSchedules.vue'
import Profile from '../views/Profile.vue'
import CourseSelection from '../views/student/CourseSelection.vue'
import StudentSchedule from '../views/student/StudentSchedule.vue'
const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      requiresAuth: false
    }
  },
  {
    path: '/',
    component: Layout,
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: {
          title: '仪表板'
        }
      },
        {
          path: '/course/template',
          name: 'CourseTemplateManagement',
          component: CourseTemplateManagement,
          meta: {
            title: '课程模板管理'
          }
        },

        {
          path: '/course/schedule',
          name: 'ScheduleManagement',
          component: ScheduleManagement,
          meta: {
            title: '课程表管理'
          }
        },
        {
          path: '/course/all-schedules',
          name: 'AllSchedules',
          component: AllSchedules,
          meta: {
            title: '所有课程表'
          }
        },
        {
          path: '/student/schedule',
          name: 'StudentSchedule',
          component: StudentSchedule,
          meta: {
            title: '学生课表'
          }
        },
      {
        path: '/system/organization',
        name: 'OrganizationManagement',
        component: OrganizationManagement,
        meta: {
          title: '组织管理'
        }
      },
    ]
  }
]
const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  if (to.meta.requiresAuth) {
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    if (token && to.path === '/login') {
      next('/dashboard')
    } else {
      next()
    }
  }
})
export default router 